{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-daa0acf248678b25",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Shortcuts\n",
    "\n",
    "Shortcut | Command\n",
    "--- | ---\n",
    "Shift+Tab | Open Help\n",
    "Strg+Enter | Run cell\n",
    "Shift+Enter | Run cell and select next cell\n",
    "Alt+Enter | Run cell and insert cell below\n",
    "Strg+# | Convert marked lines into comments (German Keyboard)\n",
    "a | Insert cell above marked cell\n",
    "b | Insert cell below marked cell\n",
    "m | Convert cell to markdown type\n",
    "y | Convert cell to code type\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-019787e0536d003e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Datatypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4dcd020cfa89b540",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "integer_name = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3df6aef2784fcde8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "absj\n",
      "Integer value: 1\n"
     ]
    }
   ],
   "source": [
    "print(integer_name)\n",
    "floatingpoint = 1.5\n",
    "\n",
    "complexnumber = 1 + 1j\n",
    "\n",
    "string1 = 'absj'\n",
    "string2 = \"ab'sj\"\n",
    "string3 = '''absj'''\n",
    "string4 = \"\"\"absj\"\"\"\n",
    "print(string4)\n",
    "\n",
    "print(f'Integer value: {integer_name}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-43c90d93065bfa05",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Arithmetic operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8135f83b973b1567",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 + 1\n",
    "2 - 1\n",
    "#2 ** 2\n",
    "#10_000\n",
    "#2*10e4\n",
    "#1*10**4\n",
    "#41/2\n",
    "#40//2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-eebdde86f62071e8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e941617baf2a039b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '123', [1, 2, 3, 4], (1+1j)]\n"
     ]
    }
   ],
   "source": [
    "# List\n",
    "\n",
    "l = [integer_name, '123', [1, 2, 3,4 ], 1 + 1j]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-060aeed43e4a6a13",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, '123', [1, 2, 3, 4], (1+1j), 5]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append(5)\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-600db0325040c35a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, '123', [1, 2, 3, 4], (1+1j), 5, 30, 40]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = [30, 40]\n",
    "l.extend(m)\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-308b7521d915b7a5",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "40\n",
      "[1, '123', [1, 2, 3, 4], (1+1j), 5, 30]\n",
      "[1, [1, 2, 3, 4], 5]\n",
      "[1, [1, 2, 3, 4], 5, 40]\n"
     ]
    }
   ],
   "source": [
    "# Indexing and Slicing\n",
    "\n",
    "print(l[0])\n",
    "print(l[-1])\n",
    "print(l[0:-1])\n",
    "print(l[0:-1:2])\n",
    "print(l[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-efbeabee80d0665c",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "# Tuple\n",
    "t = (1, 2, 3)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-476c81f5a97c7925",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 9, 8, 7)\n",
      "(1, 2, 3, 1, 2, 3, 1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "# Collection operations (Be careful):\n",
    "t2  = (9, 8, 7)\n",
    "print(t + t2)\n",
    "print(t * 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-237a8c10072cb49f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-45b86524ef35df72",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 2: 'b', 'c': 3}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary = {\n",
    "    'a': 1,\n",
    "    2: 'b',\n",
    "    'c': 3,\n",
    "}\n",
    "dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-03ec489ef2411990",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2a9786f2f18556fa",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['a', 2, 'c'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a34f021e89aec1c2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([1, 'b', 3])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-44d309a15a6bfb00",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('a', 1), (2, 'b'), ('c', 3)])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5b6a328a4568670b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "dictionary = {}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-91f075a04d39af81",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-413e118302f579f5",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 6\n",
    "if a <= 2:\n",
    "    b = 2\n",
    "elif a < 10:\n",
    "    b = 3\n",
    "else:\n",
    "    b = 4\n",
    "b "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d791d71d3f51f506",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "if (a == 2) and (b == 3):\n",
    "    c = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d89560ba654e22c0",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "d = None\n",
    "if a == 2 or d is None:\n",
    "    if d is not None:\n",
    "        c = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e097824630408777",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f22fd07d76e2bf1c",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "0\n",
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "0\n",
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "[0, 1, 4, 9, 16, 0, 1, 4, 9, 16, 0, 1, 4, 9, 16]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = list(range(5))\n",
    "l2 = []\n",
    "for entry in l:\n",
    "    print(entry ** 2)\n",
    "    l2.append(entry ** 2)\n",
    "\n",
    "for index in range(len(l)):\n",
    "    print(l[index] ** 2)\n",
    "    l2.append(l[index] ** 2)\n",
    "    \n",
    "for index, entry in enumerate(l):\n",
    "    print(entry ** 2)\n",
    "    l2.append(entry ** 2)\n",
    "    \n",
    "print(l2)\n",
    "    \n",
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-caad14285e303e07",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# List comprehensions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2bb6a18fd4059c95",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]\n"
     ]
    }
   ],
   "source": [
    "l = list(range(50))\n",
    "\n",
    "l2 = [entry ** 2 for entry in l]\n",
    "# print(l2)\n",
    "l2 = [\n",
    "    entry ** 2\n",
    "    for entry in l\n",
    "    if entry ** 2 >= 100\n",
    "]\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6e01f751959a2cfb",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Dict comprehensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2892bbc072950fc2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{10: 100,\n",
       " 11: 121,\n",
       " 12: 144,\n",
       " 13: 169,\n",
       " 14: 196,\n",
       " 15: 225,\n",
       " 16: 256,\n",
       " 17: 289,\n",
       " 18: 324,\n",
       " 19: 361,\n",
       " 20: 400,\n",
       " 21: 441,\n",
       " 22: 484,\n",
       " 23: 529,\n",
       " 24: 576,\n",
       " 25: 625,\n",
       " 26: 676,\n",
       " 27: 729,\n",
       " 28: 784,\n",
       " 29: 841,\n",
       " 30: 900,\n",
       " 31: 961,\n",
       " 32: 1024,\n",
       " 33: 1089,\n",
       " 34: 1156,\n",
       " 35: 1225,\n",
       " 36: 1296,\n",
       " 37: 1369,\n",
       " 38: 1444,\n",
       " 39: 1521,\n",
       " 40: 1600,\n",
       " 41: 1681,\n",
       " 42: 1764,\n",
       " 43: 1849,\n",
       " 44: 1936,\n",
       " 45: 2025,\n",
       " 46: 2116,\n",
       " 47: 2209,\n",
       " 48: 2304,\n",
       " 49: 2401}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2 = {\n",
    "    entry: entry ** 2\n",
    "    for entry in l\n",
    "    if entry ** 2 >= 100\n",
    "}\n",
    "d2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9be569418ecf1e2b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Collections exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7a68762a97db6b60",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Create a list of 10 elements consisting of the first 10 alphabet letters\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-b38aade7be78bf03",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "l = [l for l in 'abcdefghij']\n",
    "l\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a3b88ea5a37d3e6f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Create another list that contains the 10 elements from above in reversed order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-152f0f12ecd564f9",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "l_rev = list(reversed(l))\n",
    "l_rev\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-901ec31b5038d6d8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Create a dictionary with two key-value pairs.\n",
    "First key 'A' contains the first list with 10 alphabet letters in correct order, and second key 'B' contains the second list with the reversed order letters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-dfc2f5df094c10c2",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],\n",
       " 'B': ['j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "alph_d = {'A': l, 'B': l_rev}\n",
    "alph_d\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8d4e4929c9d77ec9",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Create three tuples of size 1 and add them to a list. Each tuple shall contain a dictionary with 5 keys that are enumerated integers and the corresponding values are these integers times ten.\n",
    "Each tuple's dictionary is continuing the enumeration of the previous dictionary.\n",
    "Print each key and value pair by iterating through the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6fd43f871ac9bac9",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key 0: value 0\n",
      "key 1: value 10\n",
      "key 2: value 20\n",
      "key 3: value 30\n",
      "key 4: value 40\n",
      "key 5: value 50\n",
      "key 6: value 60\n",
      "key 7: value 70\n",
      "key 8: value 80\n",
      "key 9: value 90\n",
      "key 10: value 100\n",
      "key 11: value 110\n",
      "key 12: value 120\n",
      "key 13: value 130\n",
      "key 14: value 140\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "res = []\n",
    "for i in range(1, 4):\n",
    "    start, end = (i-1)*5, i*5\n",
    "    d = {idx: 10*idx for idx in range(start, end)}\n",
    "    res.append((d,))\n",
    "for r in res:\n",
    "    for k, v in r[0].items():\n",
    "        print(f'key {k}: value {v}')\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-346eeb773872f4ef",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8303b3bc3c0369f1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "-1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def sub(a_val, b_val):\n",
    "    \"\"\"Calculate a - b\"\"\"\n",
    "    return a_val - b_val\n",
    "\n",
    "a = 2\n",
    "b = 1\n",
    "\n",
    "print(sub(a, b))\n",
    "print(sub(b, a))\n",
    "print(sub(b_val=b, a_val=a))\n",
    "print(sub(a_val=a, b_val=b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ad78de9fa027c230",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a8132e5ab2359dd3",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "class Dog:\n",
    "    \"\"\"Represents a dog.\"\"\"\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        \n",
    "    def sit(self):\n",
    "        print(self.name, 'is sitting.')\n",
    "        \n",
    "dog = Dog('Lucky')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9d04c3054a090f12",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lucky is sitting.\n"
     ]
    }
   ],
   "source": [
    "dog.sit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ce67bb9caa79a523",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Function and Classes exercise\n",
    "Write a class `Cat` with an init function that takes `breed` as argument and sets the same attribute.\n",
    "\n",
    "If the `breed` argument is neither 'Balinese' nor 'Bengal', an error statement is to be printed.\n",
    "\n",
    "Give the cat a function `pet()`: If the cat is balinese, print 'meow', if the cat is bengal, print 'purr'.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-cc4caad8e103d54f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "class Cat:\n",
    "    \n",
    "    def __init__(self, breed):\n",
    "        assert breed in ['Balinese', 'Bengal'], 'breed not understood'\n",
    "        self.breed = breed\n",
    "        \n",
    "    def pet(self):\n",
    "        if self.breed == 'Balinese':\n",
    "            print('meow')\n",
    "        elif self.breed == 'Bengal':\n",
    "            print('purr')\n",
    "        else:\n",
    "            print('??')\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e1c5930ef6822a17",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Create a class `CrazyCatLady` which initializes without arguments an attribute `cats`, which is a list of 10 balinese and 5 bengal cats.\n",
    "\n",
    "Create a class-function `throw`, which removes one cat from her set of cats.\n",
    "\n",
    "Create a class-function `talk_to_cats`, which returns a dictionary of her remaining cats with keys being random names and values being the corresponding cat. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2474b2db211decf9",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Max': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Sam': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Misty': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Chloe': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Smokey': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Oscar': <__main__.Cat object at 0x7ff1dac2cdf0>, 'Lucy': <__main__.Cat object at 0x7ff1dac2c0a0>, 'Molly': <__main__.Cat object at 0x7ff1dac2c0a0>}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'Simba': <__main__.Cat at 0x7ff1dac2c0a0>,\n",
       " 'Smokey': <__main__.Cat at 0x7ff1dac2c0a0>,\n",
       " 'Lucy': <__main__.Cat at 0x7ff1dac2c0a0>,\n",
       " 'Chloe': <__main__.Cat at 0x7ff1dac2c0a0>,\n",
       " 'Max': <__main__.Cat at 0x7ff1dac2c0a0>,\n",
       " 'Missy': <__main__.Cat at 0x7ff1dac2c0a0>}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "### BEGIN SOLUTION\n",
    "class CrazyCatLady:\n",
    "    \n",
    "    cat_names = ['Oscar', 'Max', 'Tiger', 'Sam', 'Misty', 'Simba', \n",
    "                 'Coco'\t, 'Chloe', 'Lucy' ,  'Missy', 'Molly', 'Tigger', \n",
    "                 'Smokey', 'Milo', 'Cleo']\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.cats = 10*[Cat('Balinese')] + 5*[Cat('Bengal')]\n",
    "    \n",
    "    def throw(self):\n",
    "        self.cats.pop()\n",
    "    \n",
    "    def talk_to_cats(self):\n",
    "       return {n: cat for n, cat in zip(random.choices(self.cat_names, k=len(self.cats)), self.cats)}\n",
    "        \n",
    "### END SOLUTION\n",
    "ccl = CrazyCatLady()\n",
    "ccl.throw()\n",
    "ccl.throw()\n",
    "ccl.throw()\n",
    "ccl.throw()\n",
    "print(ccl.talk_to_cats())\n",
    "ccl.throw()\n",
    "ccl.throw()\n",
    "ccl.throw()\n",
    "ccl.talk_to_cats()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-b838144fd880432d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Outlook numerical computing\n",
    "\n",
    "Matrix Multiplication.\n",
    "Program the multiplication of the matrices with `for` loops:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7809078b51b1f9a1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[58, 64], [139, 154]]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "]\n",
    "B = [\n",
    "    [7, 8],\n",
    "    [9, 10],\n",
    "    [11, 12],\n",
    "]\n",
    "C = [\n",
    "    [0, 0],\n",
    "    [0, 0],\n",
    "]\n",
    "\n",
    "# C = A * B\n",
    "for i, a_ in enumerate(A):\n",
    "    for a, b_ in zip(a_, B):\n",
    "        for j, b in enumerate(b_):\n",
    "            C[i][j] += a * b\n",
    "\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-50f07c286919f4f0",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 58,  64],\n",
       "       [139, 154]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.matmul(A, B)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Create Assignment",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
